package org.example.ychdp.service;

import org.example.ychdp.dao.SeatDao;
import org.example.ychdp.entity.Seat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SeatService {
    @Autowired
    private SeatDao seatDao;

    public Seat insert (Seat seat){
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();
        seat.setSelectedtime(currentDate);

        Random random = new Random();

        int row = random.nextInt(9)+1;
        int column = random.nextInt(99)+1;
        seat.setSeatrow(row);
        seat.setSeatcolumn(column);
        String number = row + "排" + column + "列";
        seat.setSeatnumber(number);

        seat.setIsavailable(false);

        seatDao.insert(seat);

        return selectSeat(seat);
    }
    public Seat selectSeat(Seat seat) {
        List<Seat> seats = seatDao.selectSeat();
        for (Seat s : seats) {
            if (s.getSeatarea().equals(seat.getSeatarea()) &&
                    s.getSeatnumber().equals(seat.getSeatnumber()) &&
                    s.getTicketid().equals(seat.getTicketid())) {
                return s;
            }
        }
        return null; // 如果没有找到，返回 null
    }
    public Seat selectByPrimaryKey(Integer id){
        return seatDao.selectByPrimaryKey(id);
    }

}
